package org.daidai.abstrusewriting.homepage.top.adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.top.TopListActivity;
import org.daidai.abstrusewriting.homepage.top.model.ListItem;
import org.daidai.abstrusewriting.homepage.top.model.RankingList;

import java.util.List;

/**
 * Created by liuch on 2016/10/31.
 */

public class RankingListAdapter extends BaseAdapter {

    private static Context context;
    private List<RankingList> lists;

    public RankingListAdapter(Context context, List<RankingList> lists) {
        this.context = context;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        if (lists != null) {
            return lists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (lists != null) {
            return lists.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(context).inflate(R.layout.list_top, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder(ret);
            ret.setTag(holder);
        }
        RankingList rankingList = lists.get(position);

        holder.bindView(rankingList);

        return ret;
    }

    public static class ViewHolder implements AdapterView.OnItemClickListener {

        private final TextView listTitle;
        private final ListView listView;
        private RankingList rankingList;

        public ViewHolder(View item) {
            listTitle = (TextView) item.findViewById(R.id.top_list_title);
            listView = (ListView) item.findViewById(R.id.top_list_list);
            listView.setOnItemClickListener(this);
        }

        public void bindView(RankingList rankingList){
            this.rankingList = rankingList;
            ListItemAdapter adapter = new ListItemAdapter(context, rankingList.getList());
            listTitle.setText(rankingList.getTitle());
            listView.setAdapter(adapter);
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String key = rankingList.getList().get(position).getKey();
            Intent intent = new Intent(context, TopListActivity.class);
            intent.putExtra("key" , key);
            context.startActivity(intent);
        }
    }
}
